using Core.SimpleTemp.Common;
using Core.SimpleTemp.Common.PagingQuery;
using Core.SimpleTemp.Entitys;
using Core.SimpleTemp.Repositories;
using Core.Toolkit.AutoMapperExtend.DataDictionary;
using Core.Toolkit.Common;
using Core.Toolkit.Core;
using Core.Toolkit.Core.Api;
using Core.Toolkit.Core.Application;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Core.SimpleTemp.Application
{
    [AutoDi()]
    [AutoDi(typeof(IConcurrentDataDicProvider))]
    public class YH_BasisTypeService : BaseAppService<YH_BasisTypeDto, YH_BasisType, YH_BasisTypeRepository>, IConcurrentDataDicProvider
    {
        readonly ExcelHelp _excelHelp;
        public YH_BasisTypeService(YH_BasisTypeRepository repository, ExcelHelp excelHelp) : base(repository)
        {
            _excelHelp = excelHelp;
        }

        private LoadPageOffsetModelAuto<YH_BasisType, YH_BasisTypeDto> GetLoadPageOffsetMode(PagingModel<YH_BasisTypeDto> pagingQueryModel)
        {
            var loadPageOffsetModel = this.GetLoadPageOffsetModelAuto(pagingQueryModel);
            var baseQuery = _repository.QueryBase().AsNoTracking();
            loadPageOffsetModel.QueryWhere = this.CreateQRList()
             .Add("Name", "=")
             .Add("Pid", "=")
             .Add("Order", "=")
             .Add("Remark", "=")
             .Add("CreateFactory", "=")
             .Add("CreateDept", "=")
             .Add("UpdateDept", "=");

            loadPageOffsetModel.BasqQuery = baseQuery;
            loadPageOffsetModel.Order = orderModel => orderModel.CreateTime;
            loadPageOffsetModel.Selector = model => new YH_BasisType()
            {
                Id = model.Id,
                Name = model.Name,
                Pid = model.Pid,
                Order = model.Order,
                Remark = model.Remark,
                CreateFactory = model.CreateFactory,
                CreateDept = model.CreateDept,
                UpdateDept = model.UpdateDept,
            };
            return loadPageOffsetModel;
        }


        public async Task<IPageModel<YH_BasisTypeDto>> GetList(PagingModel<YH_BasisTypeDto> pagingQueryModel)
        {
            var loadPageOffsetModel = GetLoadPageOffsetMode(pagingQueryModel);
            return await base.LoadPageOffsetAsync(loadPageOffsetModel);
        }


        public async Task<List<YH_BasisType>> GetList(Guid? factoryId)
        {
            return await _repository.QueryBase().Where(a => a.CreateFactory == factoryId).ToListAsync();
        }


        public async Task<string> Export(PagingModel<YH_BasisTypeDto> pagingQueryModel)
        {
            var loadPageOffsetModel = GetLoadPageOffsetMode(pagingQueryModel);
            var DataList = await this.ExportDataAsync(loadPageOffsetModel);
            var excelData = DataList.Select(model => new
            {
                model.Name,
                Name_title = "基础管理类隐患类型名称",
                model.Pid,
                Pid_title = "上级基础管理类隐患类型ID",
                model.Order,
                Order_title = "排列顺序",
                model.Remark,
                Remark_title = "",
                model.CreateFactory,
                CreateFactory_title = "",
                model.CreateDept,
                CreateDept_title = "",
                model.UpdateDept,
                UpdateDept_title = "",
            }).ToList();
            return _excelHelp.Create(excelData);
        }



        #region 字典数据池热更新
        public const string DATA_DIC_KEY = "基础管理类隐患类型";

        /// <summary>
        /// 向热更新数据池添加初始数据
        /// 项目已启动调用此方法
        /// </summary>
        /// <param name="_dataDic"></param>
        /// <param name="_dataDicListByCategory"></param>
        public void LoadData(ConcurrentDictionary<string, string> _dataDic, ConcurrentDictionary<string, List<DataDicModel>> _dataDicListByCategory)
        {
            //仅初始化dataDic (用于ConcurrentKeyToValue标签)
            var userList = _repository.Set.Select(u => new { u.Id, u.Name }).ToList();
            foreach (var item in userList)
            {
                _dataDic[ConcurrentDataDicPool.CreateKey(DATA_DIC_KEY, item.Id.ToString())] = item.Name ?? "";
            }
            _dataDicListByCategory[DATA_DIC_KEY] = GetDicModelList();
        }

        private List<DataDicModel> GetDicModelList()
        {
            var userList = _repository.Set.Select(u => new { u.Id, u.Name }).ToList();
            List<DataDicModel> dataDicModelList = new List<DataDicModel>();
            foreach (var item in userList)
            {
                dataDicModelList.Add(new DataDicModel
                {
                    Category = DATA_DIC_KEY,
                    Key = item.Id.ToString(),
                    Value = item.Name
                });
            }
            return dataDicModelList;
        }

        /// <summary>
        /// 热更新DataDic池
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="Name"></param>
        private void UpdateDataDidcPoll(string userId, string Name)
        {
            var key = ConcurrentDataDicPool.CreateKey(DATA_DIC_KEY, userId.ToString());
            ConcurrentDataDicPool.UpdateDataDic(key, Name);
            ConcurrentDataDicPool.UpdateCategory(DATA_DIC_KEY, GetDicModelList());
        }
        #endregion

    }

}
